#include <myheader_c_plus.h>
using std::cout;
using std::endl;
using std::ostream;
using std::istream;
using std::cin;
class Complex;
class Point{
public:
    int distance(Complex & c);

};
class Complex{
private:
    int _real;
    int _image;
public:
    Complex(int real, int image)
    :_real(real)
    ,_image(image)
    {

    }


    //友元函数
    friend ostream & operator<<(ostream & os, const Complex & rhs);
    friend istream & operator>>(istream & is, Complex & rhs);
    friend int  Point::distance(Complex & c);

};
int Point::distance(Complex & c){
    int distance;
    return distance = c._real + c._image;
}
//运算符重载
ostream & operator<<(ostream & os, const Complex & rhs){
    os << "real:" << rhs._real << "image:" << rhs._image << endl;
    return os;
}
void read(istream & is, int & num){
    while(1){
        is >> num;
        //判断流的状态
        if(is.bad()){
            cout << "当前流已经破坏" << endl;
            break;
        }else if(is.fail()){
            cout << "输入数字不合法,请重新输入" << endl;
            is.clear();
            is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }else{
            break;
        }
    }
}
istream & operator>>(istream & is,  Complex & rhs){
    cout << "请输入实部" << endl;
    read(is, rhs._real);
    cout << "请输入虚部" << endl;
    read(is, rhs._image);
    return is;

}

int main(int argc, char *argv[])
{
    Complex cp(1,3);
    cout << cp << endl;
    cin >> cp;
    cout << cp << endl;
    return 0;
}

